using System;
using System.Collections.Generic;

namespace Quiki.ExpressionParsers
{
    public class InternalLinkExpressionParser : BinaryWikiTokenInlineExpressionParser
    {
        public InternalLinkExpressionParser ()
            : base ("[[", "]]", ElementConstructor)
        {
        }

        private static WikiTextElement ElementConstructor (string text)
        {
            string[] splits = text.Split ('|');

            WikiDocReference reference = new WikiDocReference (splits[0]);
            if (reference.FirstPart == "Image")
                return CreateImage(splits);

            WikiInternalLinkElement element = new WikiInternalLinkElement ();
            element.Reference = reference;
            if (splits.Length > 1)
                element.LinkText = splits[1].Trim ();

            return element;
        }

        private static WikiTextElement CreateImage(string[] splits)
        {
            WikiDocReference reference = new WikiDocReference (splits[0]);
            WikiImageElement element = new WikiImageElement();
            element.Name = reference.LastPart;

            List<string> parameters = new List<string>();
            for (int i = 1; i < splits.Length; i++)
                parameters.Add(splits[i].Trim());

            element.ImageParameters = parameters;

            return element;
        }
    }
}